package com.wyj.wuliwuli.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtil {

    /**
     * 当前时间
     * @return 当前时间
     */
    public static Date now() {
        return Calendar.getInstance().getTime();
    }

    /**
     * 时分秒为0的当前时间
     * @return 时分秒为0的当前时间
     */
    public static Date cuteNow() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 将日期时分秒设为0
     * @param date
     * @return
     */
    public static Date cuteDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 按默认格式格式化日期：yyyy-MM-dd HH:mm:ss
     * @param date 日期
     * @return
     */
    public static String format(Date date) {
        if (null == date) {
            return "";
        }
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateFormat.format(date);
    }

    /**
     * 按指定格式格式化日期
     * @param date 日期
     * @param pattern 格式：如：yyyy-MM-dd
     * @return
     */
    public static String format(Date date, String pattern) {
        DateFormat dateFormat = new SimpleDateFormat(pattern);
        return dateFormat.format(date);
    }

    /**
     * 按默认格式解析为日期：yyyy-MM-dd HH:mm:ss
     * @param date
     * @return
     */
    public static Date parse(String date) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return dateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 按指定格式解析为日期
     * @param date
     * @param pattern 格式：如：yyyy-MM-dd
     * @return
     */
    public static Date parse(String date, String pattern) {
        DateFormat dateFormat = new SimpleDateFormat(pattern);
        try {
            return dateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 日期加天
     * @param date
     * @param days
     * @return
     */
    public static Date addDays(Date date, int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + days);
        return calendar.getTime();
    }

    /**
     * 日期减天
     * @param date
     * @param days
     * @return
     */
    public static Date subtractDays(Date date, int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - days);
        return calendar.getTime();
    }

    /**
     * int格式的年月日
     * @param date 日期
     * @return int格式年月日
     */
    public static Integer formatDateIntValue(Date date) {
        String dateStr = format(date, "yyyyMMdd");
        return Integer.parseInt(dateStr);
    }

    /**
     * end比start多的天数
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return
     */
    public static Integer differentDays(Date startDate, Date endDate) {
        try {
            Calendar start = Calendar.getInstance();
            start.setTime(startDate);
            start.set(Calendar.HOUR_OF_DAY, 0);
            start.set(Calendar.MINUTE, 0);
            start.set(Calendar.SECOND, 0);
            Calendar end = Calendar.getInstance();
            end.setTime(endDate);
            end.set(Calendar.HOUR_OF_DAY, 1);
            end.set(Calendar.MINUTE, 0);
            end.set(Calendar.SECOND, 0);

            long startMillis = start.getTimeInMillis();
            long endMillis = end.getTimeInMillis();

            Long biggerTimes = (endMillis - startMillis) / (1000 * 3600 * 24);
            return biggerTimes.intValue();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * start 和 end 间隔时间天数
     * @param startDate
     * @param endDate
     * @return
     */
    public static Integer intervalDays(Date startDate, Date endDate) {
        try {
            return differentDays(startDate, endDate);
        } catch (Exception e) {
            return null;
        }
    }

}
